<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Ajax extends CI_Controller {

    function do_prune() {
        if ($this->session->userdata('user_id')) {
            // Update Campaign Stat //
            $current_time = time();


            // Pending to Starting //
            $this->db->set('status', 'starting')
                    ->where('starting_at <= ', $current_time)
                    ->where('status', 'pending')
                    ->update('campaigns');

            // Starting to Stopped //
            $this->db->set('status', 'stopped')
                    ->where('stopped_at <= ', $current_time)
                    ->where('status', 'starting')
                    ->where('candidate_amount > ', 0)
                    ->update('campaigns');
        }
    }

    function do_update_css_preset() {
        if ($this->session->userdata('is_admin') != 1)
            die();

        $sponsor_id = $this->input->post('sponsor_id');
        $css = $this->input->post('css');



        $this->db->set('css_preset', $css)->where('id', $sponsor_id)->update('sponsors');

        // Delete Cache //
        $this->load->driver('cache', array('adapter' => 'file'));
        $this->cache->delete('get_campaign_by_hash_' . $this->input->post('hash'));
    }

    function do_vote_up() {
        if ($this->session->userdata('user_id')) {
            $campaign_id = hashids_decrypt($this->input->post('hash'));
            $candidate_id = floor($this->input->post('candidate_id'));

            // Select campaign_id //
            if ($campaign_id) {
                $row = $this->db->select('id')->where('id', $campaign_id)->where('status', 'starting')->get('campaigns')->row_array();
                if ($row) {
                    $campaign_id = $row['id'];

                    // Check Existing //
                    if ($this->db->where('user_id', $this->session->userdata('user_id'))->where('positive_vote', 0)->where('negative_vote', 0)->where('campaign_join_id', $candidate_id)->count_all_results('campaign_activities') > 0) {

                        $this->db->set('positive_vote', 1)
                                ->set('voted_at', time())
                                ->where('user_id', $this->session->userdata('user_id'))
                                ->where('campaign_join_id', $candidate_id)
                                ->update('campaign_activities');

                        // Update Campaign Sum //
                        $this->db->set('sum_positive_score', 'sum_positive_score+1', FALSE)->where('campaign_id', $campaign_id)->update('campaign_joins');
                    }
                }
            }
        }
    }

    function do_vote_down() {
        if ($this->session->userdata('user_id')) {
            $campaign_id = hashids_decrypt($this->input->post('hash'));
            $candidate_id = floor($this->input->post('candidate_id'));

            // Select campaign_id //
            if ($campaign_id) {
                $row = $this->db->select('id')->where('id', $campaign_id)->where('status', 'starting')->get('campaigns')->row_array();
                if ($row) {
                    $campaign_id = $row['id'];

                    // Check Existing //
                    if ($this->db->where('campaign_id', $campaign_id)->where('positive_vote', 0)->where('negative_vote', 0)->where('user_id', $this->session->userdata('user_id'))->where('campaign_join_id', $candidate_id)->count_all_results('campaign_activities') > 0) {

                        $this->db->set('negative_vote', 1)
                                ->set('voted_at', time())
                                ->where('user_id', $this->session->userdata('user_id'))
                                ->where('campaign_join_id', $candidate_id)
                                ->update('campaign_activities');
                        // Update Campaign Sum //
                        $this->db->set('sum_negative_score', 'sum_negative_score+1', FALSE)->where('campaign_id', $campaign_id)->update('campaign_joins');
                    }
                }
            }
        }
    }

    function do_comment_count() {
        if ($this->session->userdata('user_id')) {
            $candidate_id = floor($this->input->post('candidate_id'));
            // Check Existing //
            if ($this->db->where('user_id', $this->session->userdata('user_id'))->where('campaign_join_id', $candidate_id)->count_all_results('campaign_activities') > 0) {
                $this->db->set('comment', 'comment+1', FALSE)
                        ->set('actioned_at', time())
                        ->where('user_id', $this->session->userdata('user_id'))
                        ->where('campaign_join_id', $candidate_id)
                        ->update('campaign_activities');
                // Update Campaign Sum //
                //$this->db->set('sum_negative_score', 'sum_negative_score+1', FALSE)->where('campaign_id', $campaign_id)->update('campaign_joins');
            }
        }
    }

    // Update Sum View , Wilson
    private function _wilsonScore($up, $down) {

        $score = (($up + 1.9208) / ($up + $down) - 1.96 * sqrt(($up * $down) /
                        ($up + $down) + 0.9604) / ($up + $down)) / (1 + 3.8416 / ($up + $down));
        return round($score * 100, 4);
    }

    function do_sum_candidate() {
        if ($this->session->userdata('user_id')) {
            $candidate_id = floor($this->input->post('candidate_id'));


            $row = $this->db->select('SUM(view) as sum_view, SUM(positive_vote) as sum_positive_score, SUM(negative_vote) as sum_negative_score')->where('campaign_join_id', $candidate_id)->get('campaign_activities')->row_array();

            if (isset($row['sum_view'])) {

                // Calculate Wilson //
                $wilson = $this->_wilsonScore($row['sum_positive_score'], $row['sum_negative_score']);

                // Update Candidate //
                $this->db->set('sum_wilson_score', $wilson)
                        ->set('sum_view', $row['sum_view'])
                        ->set('sum_positive_score', $row['sum_positive_score'])
                        ->set('sum_negative_score', $row['sum_negative_score'])
                        ->where('id', $candidate_id)
                        ->update('campaign_joins');
            }
        }
    }

    function do_watch_highlight() {
        
        $hash = $this->input->post('hash');
        $token = $this->input->post('token');
        
        if (hashids_decrypt($hash))
        {
            session_start();
            if ($_SESSION["highlight_" . $hash] == $token)
            {
                $_SESSION["highlight_" . $hash] = TRUE;
            }
        }
        
    }

}
